/*
 * Copyright 2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.pajamas.daydream.wrapper.commons.jackson;

import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.ser.std.ReferenceTypeSerializer;
import com.fasterxml.jackson.databind.type.ReferenceType;
import com.fasterxml.jackson.databind.util.NameTransformer;

import org.pajamas.protocol.Wrapper;

/**
 * @author william
 * @since 2023/12/31
 */

public class CommonsWrapperSerializer
        extends ReferenceTypeSerializer<Wrapper<?>> {
    private static final long serialVersionUID = 7033056117926756985L;

    protected CommonsWrapperSerializer(ReferenceType fullType, boolean staticTyping,
                                       TypeSerializer vts, JsonSerializer<Object> ser) {
        super(fullType, staticTyping, vts, ser);
    }

    protected CommonsWrapperSerializer(CommonsWrapperSerializer base, BeanProperty property,
                                       TypeSerializer vts, JsonSerializer<?> valueSer, NameTransformer unwrapper,
                                       Object suppressableValue, boolean suppressNulls) {
        super(base, property, vts, valueSer, unwrapper,
                suppressableValue, suppressNulls);
    }

    @Override
    protected ReferenceTypeSerializer<Wrapper<?>> withResolved(BeanProperty prop,
                                                               TypeSerializer vts, JsonSerializer<?> valueSer,
                                                               NameTransformer unwrapper) {
        return new CommonsWrapperSerializer(this, prop, vts, valueSer, unwrapper,
                _suppressableValue, _suppressNulls);
    }

    @Override
    public ReferenceTypeSerializer<Wrapper<?>> withContentInclusion(Object suppressableValue,
                                                                    boolean suppressNulls) {
        return new CommonsWrapperSerializer(this, _property, _valueTypeSerializer,
                _valueSerializer, _unwrapper,
                suppressableValue, suppressNulls);
    }

    @Override
    protected boolean _isValuePresent(Wrapper<?> value) {
        return value.isPresent();
    }

    @Override
    protected Object _getReferenced(Wrapper<?> value) {
        return value.unwrap();
    }

    @Override
    protected Object _getReferencedIfPresent(Wrapper<?> value) {
        return value.isPresent() ? value.unwrap() : null;
    }
}
